import { isEmail, isPhoneNumber } from "@/utils";

class Type {
  static USERNAME = "username"
  static PHONE = "phone"
  static EMAIL = "email"
  static EMAIL_CODE = "emailcode"
  static PASSWORD = "password"
  static CONFIRME_PASSWORD = "confirmPassword"
  static INVITE_CODE = 'inviteCode'
}

const valueCheck = (value, type, password) => {
  if (type === Type.USERNAME) {
    if (!value) {
      return {
        value,
        err: "姓名不能为空",
      };
    }
    if (value.length < 2 || value.length > 20) {
      return {
        value,
        err: "姓名长度为2-20字符",
      };
    }
    return {
      value,
      err: value ? null : "姓名不能为空",
    };
  }
  if (type === Type.PHONE) {
    if (!value) {
      const err = "手机号不能为空";
      return {
        value,
        err,
      };
    }
    const isValid = isPhoneNumber(value);
    return {
      value,
      err: isValid ? null : "手机号不符合规范",
    };
  }
  if (type === Type.EMAIL) {
    if (!value) {
      const err = "邮箱不能为空";
      return {
        value,
        err,
      };
    }
    const isValid = isEmail(value);
    return {
      value,
      err: isValid ? null : "邮箱格式不符合规范",
    };
  }
  if (type === Type.PASSWORD) {
    const err = value ? null : "密码是必填项";
    return {
      value,
      err,
    };
  }
  if (type === Type.CONFIRME_PASSWORD) {
    if (!value) {
      const err = "密码是必填项";
      return {
        value,
        err,
      };
    }
    if (value !== password) {
      const err = "两次输入密码不一致";
      return {
        value,
        err,
      };
    }
    return {
      value,
      err: value ? null : "密码是必填项",
    };
  }
  if(type === Type.INVITE_CODE){
    if (!value) {
      const err = "邀请码是必填项";
      return {
        value,
        err,
      };
    }
    return {
      value
    }
  }
  if(type === Type.EMAIL_CODE){
    if (!value) {
      const err = "验证码是必填项";
      return {
        value,
        err,
      };
    }
    return{
      value
    }
  }
  return {
    value: "",
    err: null,
  };
};

const trimValue = (e) => e.target.value


export { valueCheck, trimValue, Type };
